import 'package:cached_network_image/cached_network_image.dart';
import 'package:du_application/provide/AffairDetailProvider.dart';
import 'package:du_application/routes/application.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provide/provide.dart';

class AffairDetail extends StatelessWidget {
  final int id;
  const AffairDetail({Key key,this.id}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Provide<AffairDetailProvider>(
      builder: (context,child,provide){
      return provide==null||provide.affairDetailModel==null?Scaffold(): Scaffold(
      appBar: AppBar(
          title: Container(
              child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Row(children: [
            Container(
              child: CircleAvatar(
                backgroundImage:
                    NetworkImage(provide.affairDetailModel.data.userInfo.avatarUrl),
              ),
            ),
            Container(
              padding: EdgeInsets.all(5),
              child: Text(provide.affairDetailModel.data.userInfo.nickName),
            ),
          ]),
         provide.affairDetailModel.data.isMe!=null&&!provide.affairDetailModel.data.isMe?FlatButton.icon(
              onPressed: (){
                 provide.follow(context, provide.affairDetailModel.data.info.userId);
              },
              icon: Icon(Icons.add, color: Colors.cyan[400],size:ScreenUtil.getInstance().setSp(20) ,),
              label: Text("${provide.affairDetailModel.data.follow==false?'关注':'取消关注'}", style: TextStyle(color: Colors.cyan[400],fontSize: ScreenUtil.getInstance().setSp(20)))):Container(),
        ],
      ))),
      body: Container(
          child: ListView(
        children: <Widget>[
          Container(
              child: CachedNetworkImage(
            imageUrl: "${provide.affairDetailModel.data.info.url}",
            fit: BoxFit.fitWidth,
            placeholder: (context, url) => Image.asset('assets/loading.jpg'),
            // print("111");
          )),
          Container(
            padding: EdgeInsets.symmetric(
                horizontal: ScreenUtil().setWidth(20),
                vertical: ScreenUtil().setWidth(10)),
            alignment: Alignment.centerLeft,
            child: Text(
              "${provide.affairDetailModel.data.info.content}",
              textAlign: TextAlign.start,
            ),
          ),
      provide.affairDetailModel.data.info.isGoods?Container(
              padding:
                  EdgeInsets.symmetric(horizontal: ScreenUtil().setWidth(20)),
              alignment: Alignment.center,
              child: InkWell(
                onTap: (){
                  Application.router.navigateTo(context, "/goods?id=${  provide.affairDetailModel.data.goods.id}");
                },
                child: Container(
                  padding: EdgeInsets.symmetric(
                      horizontal: ScreenUtil().setWidth(10),
                      vertical: ScreenUtil().setWidth(10)),
                  color: Colors.grey[200],
                  child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Container(
                            height: ScreenUtil().setWidth(100),
                            width: ScreenUtil().setWidth(200),
                            child: CachedNetworkImage(
                              imageUrl:
                                  provide.affairDetailModel.data.goods.picUrl,
                              fit: BoxFit.cover,
                              placeholder: (context, url) =>
                                  Image.asset('assets/loading.jpg'),
                              // print("111");
                            )),
                        Container(
                          width: ScreenUtil().setWidth(800),
                          padding: EdgeInsets.symmetric(
                              horizontal: ScreenUtil().setWidth(10)),
                          alignment: Alignment.centerLeft,
                          child: Text(
                            "${provide.affairDetailModel.data.goods.name}",
                            style: TextStyle(
                                fontWeight: FontWeight.bold,
                                color: Colors.black54),
                                overflow: TextOverflow.ellipsis,
                          ),
                          
                          
                        )
                      ]),
                )
              )):Container(),
        provide.affairDetailModel.data.affairss!=null&&provide.affairDetailModel.data.affairss.length>0?Container(
              padding: EdgeInsets.symmetric(
                  horizontal: ScreenUtil().setWidth(20),
                  vertical: ScreenUtil().setWidth(30)),
              child: Text("相关推荐",
                  style: TextStyle(fontSize: ScreenUtil().setSp(40)))):Container(),
            provide.affairDetailModel.data.affairss!=null&&provide.affairDetailModel.data.affairss.length>0? GridView.builder(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            padding:
                EdgeInsets.symmetric(horizontal: ScreenUtil().setWidth(20)),
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                //横轴元素个数
                crossAxisCount: 2,
                //纵轴间距
                mainAxisSpacing: 1,
                //横轴间距
                crossAxisSpacing: 1,
                //子组件宽高长度比例
                childAspectRatio: 1/1.35),
            itemCount: provide.affairDetailModel.data.affairss.length,
            itemBuilder: (context, i) {
              String imgPath = "${provide.affairDetailModel.data.affairss[i].affair.url}";
              return new InkWell(
              onTap: (){
   Application.router.navigateTo(context, "/affairDetail?id=${provide.affairDetailModel.data.affairss[i].affair.id}");
              },
              child: Container(
        
                child: Column(
                      children: <Widget>[
                        Stack(
                          children: <Widget>[
                            Container(
                               child: Hero(
                                tag: "${provide.affairDetailModel.data.affairss[i].affair.id}",
                                child: Stack(
                                  alignment: Alignment.bottomCenter,
                                  children: <Widget>[
                                 Container(
                                   width: ScreenUtil().setWidth(500),
                                   height: ScreenUtil().setWidth(500),
                                 child: CachedNetworkImage(
                                     imageUrl: imgPath,
                                     fit: BoxFit.contain,
                                     placeholder: (context, url) =>
                                         Image.asset('assets/loading.jpg'),
                                     // print("111");
                                   )
                                 ),
                                    Container(
                                      height: ScreenUtil().setWidth(50),
                                      decoration: new BoxDecoration(
                                        gradient: LinearGradient(
                                            begin: Alignment.bottomCenter,
                                            end: Alignment.topCenter,
                                            colors: [
                                              Color.fromRGBO(0, 0, 0, 0.5),
                                              // Color.fromRGBO(0, 0, 0, 0.5),
                                              Color.fromRGBO(0, 0, 0, 0)
                                            ]),
                                      ),
                                      //  color: Color.fromRGBO(0, 0, 0, 0.1),
                                    )
                                  ],
                                ),
                              ),
                            ),
                        Positioned(
                              bottom: ScreenUtil().setWidth(20),
                              left: ScreenUtil().setWidth(15),
                              child: Icon(
                                Icons.looks,
                                color: Colors.white,
                              ),
                            ),
                            Positioned(
                                bottom: ScreenUtil().setWidth(25),
                                left: ScreenUtil().setWidth(65),
                                child: Text(
                                  "29",
                                  style: TextStyle(color: Colors.white),
                                )),
                          ],
                        ),
                        Container(
                          margin: EdgeInsets.symmetric(
                              vertical: ScreenUtil().setSp(20)),
                          width: ScreenUtil().setWidth(500),
                        
                          child: Text(
                            "${provide.affairDetailModel.data.affairss[i].affair.content}",
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                            textAlign: TextAlign.left,
                          ),
                        ),
                      Expanded(
                    child: Container(
                           margin:
                               EdgeInsets.only(bottom: ScreenUtil().setSp(20)),
                           child: Row(
                             mainAxisAlignment: MainAxisAlignment.spaceBetween,
                             children: <Widget>[
                               Row(
                                 children: <Widget>[
                                   Container(
                                     margin: EdgeInsets.symmetric(
                                         horizontal: ScreenUtil().setSp(10)),
                                     width: ScreenUtil().setWidth(50),
                                     height: ScreenUtil().setWidth(50),
                                     child: CircleAvatar(
                                       backgroundImage: NetworkImage(
                                           "${provide.affairDetailModel.data.affairss[i].userInfo.avatarUrl}"),
                                     ),
                                   ),
                                   Text(
                                     "${provide.affairDetailModel.data.affairss[i].userInfo.nickName}",
                                     style: TextStyle(color: Colors.grey[600]),
                                   )
                                 ],
                               ),
                             ],
                           ))
                     )
                      ],
                    )
              ),
        
              );
            },
          ):Container()
        ],
      )),
    );

      },
    ); 
  }
}
